import { useEffect, useState } from "react";

export function useRequest<T extends () => Promise<unknown>>(
  fn: T
): {
  loading: boolean;
  error: boolean;
  data: Awaited<ReturnType<T>>;
} {
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(false);
  const [data, setData] = useState<any>();

  useEffect(() => {
    setLoading(true);
    fn()
      .then((res) => {
        setData(res);
      })
      .catch((err) => {
        console.log(err);
        setError(true);
      })
      .finally(() => {
        setLoading(false);
      });
  }, [fn]);

  return {
    loading,
    error,
    data,
  };
}
